package se.webbzon.boltzmann.minigame.botany;

import java.awt.Rectangle;

import se.webbzon.boltzmann.configuration.Difficulty;
import se.webbzon.boltzmann.minigame.chemistry.CarbonDioxideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.HydrogenSulfideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.NitriteMolecule;
import se.webbzon.boltzmann.minigame.chemistry.OxygenMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SandBoxMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SulfateMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SulfurDioxideMolecule;
import se.webbzon.boltzmann.minigame.chemistry.SulfuricAcidMolecule;
import se.webbzon.boltzmann.minigame.chemistry.WaterMolecule;
import se.webbzon.boltzmann.minigame.effect.SandBoxCloud;
import se.webbzon.boltzmann.minigame.effect.SandBoxRain;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxContainer;
import se.webbzon.boltzmann.minigame.sandbox.ReadOnlySandBox;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxArrow;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxItem;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxPicture;
import se.webbzon.boltzmann.resource.Resources;

public class SulfurCycle extends Cycle {
	
	/*============================================================
	Protected Methods
	============================================================*/

	@Override protected void onCreate(ReadOnlySandBox box) {
		Rectangle r = new Rectangle();
		box.getBoundary(r);
		double w = r.getWidth(), h = r.getHeight();
		
		/** Create instances **/
		DefaultSandBoxContainer h2so4, h2s, so4, so2;
		h2so4 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		h2so4.setPosition(272, 56);
		h2s = new DefaultSandBoxContainer(SandBoxMolecule.class);
		h2s.setPosition(64, 56);
		so4 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		so4.setPosition(272, 216);
		so2 = new DefaultSandBoxContainer(SandBoxMolecule.class);
		so2.setPosition(544, 56);

		SandBoxPicture plant, mountain, factory, b1, b2;
		plant = new SandBoxPicture(Resources.getImageSet("biology_plant"),50);
		plant.setPosition(272, 136);
		plant.setSize(64, 64);
		plant.setSpeed(0.02);
		mountain = new SandBoxPicture(Resources.getTextureSet("weather_mountain_01"),50);
		mountain.setSize(64, 64);
		mountain.setPosition(64, 136);
		factory = new SandBoxPicture(Resources.getTextureSet("machine_factory_01"),50);
		factory.setSize(64, 64);
		factory.setPosition(544, 136);
		b1 = new SandBoxPicture(Resources.getImageSet("biology_bacteria"),50);
		b1.setSize(32, 32);
		b1.setPosition(160, 168);
		b2 = new SandBoxPicture(Resources.getImageSet("biology_bacteria"),50);
		b2.setSize(32, 32);
		b2.setPosition(392, 168);
		
		SandBoxCloud cloud = new SandBoxCloud();
		cloud.setPosition(272, 40);
		SandBoxRain rain = new SandBoxRain();
		rain.setPosition(272, 40);
		rain.setSize(128, 128);
		Earth earth = new Earth();
		earth.setSize(earth.getWidth(), 96);
		
		/** Create arrows **/
		SandBoxArrow[] arrows = {
				new SandBoxArrow(so4,b1),
				new SandBoxArrow(b1,so4),
				new SandBoxArrow(so4,plant),
				new SandBoxArrow(b2,so4),
				new SandBoxArrow(b1,h2s),
				new SandBoxArrow(h2s,b1),
				new SandBoxArrow(h2so4,plant),
				new SandBoxArrow(so2,plant),
				new SandBoxArrow(so2,b2),
				new SandBoxArrow(mountain,h2s),
				new SandBoxArrow(mountain,so2),
				new SandBoxArrow(so2,h2so4),
				new SandBoxArrow(factory,so2)
		};
		
		/** Add items to cycle **/
		SandBoxItem[] items = {
				h2so4, h2s, so4, so2, 
				plant, mountain, factory, cloud, rain, earth,
				b1, b2
		};
		
		for (SandBoxItem item : items)
			addItem(item);
		for (SandBoxItem item : arrows)
			addItem(item);
		
		/** Create and add molecules **/
		SandBoxMolecule[] molecules = {
				new WaterMolecule(), new HydrogenSulfideMolecule(),
				new OxygenMolecule(), new SulfuricAcidMolecule(),
				new SulfateMolecule(), new SulfurDioxideMolecule(),
				new NitriteMolecule(), new CarbonDioxideMolecule()
		};
		
		for (SandBoxMolecule m : molecules) {
			m.setPosition(	0.1 + 0.8 * w * Math.random(), 
							0.1 + 0.8 * h * Math.random());
			m.setLifetime(-1);
			addItem(m);
		}
		
		/** Add solutions **/
		addSolution(h2so4,SulfuricAcidMolecule.class);
		addSolution(h2s,HydrogenSulfideMolecule.class);
		addSolution(so4,SulfateMolecule.class);
		addSolution(so2,SulfurDioxideMolecule.class);
	}

	@Override protected void onDestroy(ReadOnlySandBox box) {
		clearSolution();
		clearItems();
	}
	
	@Override public Difficulty getDifficulty() {
		return Difficulty.HARD;
	}

}
